package com.jzo2o.customer.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.customer.mapper.BankAccountMapper;
import com.jzo2o.customer.mapper.ServeProviderMapper;
import com.jzo2o.customer.mapper.WorkerCertificationAuditMapper;
import com.jzo2o.customer.model.domain.BankAccount;
import com.jzo2o.customer.model.dto.request.BankAccountUpsertReqDTO;
import com.jzo2o.customer.service.IBankAccountService;
import com.jzo2o.customer.service.ICommonUserService;
import com.jzo2o.mvc.utils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 银行账户 服务实现类
 * </p>
 *
 * @author wei
 * @since 2025-01-17
 */
@Service
public class BankAccountServiceImpl extends ServiceImpl<BankAccountMapper, BankAccount> implements IBankAccountService {

    @Autowired
    private ICommonUserService commonUserService;

    @Autowired
    private WorkerCertificationAuditMapper workerCertificationAuditMapper;
    @Autowired
    private ServeProviderMapper serveProviderMapper;

    @Override
    public void addBankAccount (BankAccountUpsertReqDTO bankAccountUpsertReqDTO) {

//        String name = UserContext.currentUser ().getName ();
//        if (name == null) {
//            throw new BadRequestException ("登录用户错误");
//        }
//        Long l = UserContext.currentUserId ();
//        WorkerCertificationAudit workerCertificationAudit = workerCertificationAuditMapper.selectOne (
//                Wrappers.<WorkerCertificationAudit>lambdaQuery ()
//                        .eq (WorkerCertificationAudit::getServeProviderId, l)
//                        .eq (WorkerCertificationAudit::getCertificationStatus, CertificationStatusEnum.SUCCESS.getStatus ())
//        );
//        if (workerCertificationAudit == null) {
//            throw new BadRequestException ("未查找到用户实名数据");
//        }
//        if(workerCertificationAudit.getName ().equals (bankAccountUpsertReqDTO.getName ())){
//            throw new BadRequestException ("用户名与实名认证用户名不一致");
//        }
        BankAccount bankAccount = BeanUtils.copyBean (bankAccountUpsertReqDTO, BankAccount.class);
        bankAccount.setType (UserContext.currentUser ().getUserType ());
        bankAccount.setId (UserContext.currentUserId ());
        if (this.getById (UserContext.currentUserId ())==null) {
            // TODO
            boolean save = this.save (bankAccount);
            if (!save) {
                throw new RuntimeException ("保存失败");
            }
        } else {
            // TODO
            boolean update = this.updateById (bankAccount);
            if (!update) {
                throw new RuntimeException ("更新失败");
            }
        }

    }

    @Override
    public BankAccount getCurrentUserBankAccount () {
//        String name = UserContext.currentUser ().getName ();
//        if (name == null) {
//            throw new BadRequestException ("用户错误");
//        }

//        Long l = UserContext.currentUserId ();
//        WorkerCertificationAudit workerCertificationAudit = workerCertificationAuditMapper.selectOne (
//                Wrappers.<WorkerCertificationAudit>lambdaQuery ()
//                        .eq (WorkerCertificationAudit::getServeProviderId, l)
//                        .eq (WorkerCertificationAudit::getCertificationStatus, CertificationStatusEnum.SUCCESS.getStatus ())
//        );
//        if (workerCertificationAudit == null) {
//            throw new BadRequestException ("未查找到用户实名数据");
//        }
        return this.getById (UserContext.currentUserId ());
    }
}
